package com.hmall.order.web;

import com.hmall.common.Constant.OrderConstant;
import com.hmall.common.result.Result;
import com.hmall.order.pojo.Order;
import com.hmall.order.service.impl.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/pay")
public class PayController {
    @Autowired
    OrderService orderService;
    
    @GetMapping("/{orderId}")
    public Result pay(@PathVariable("orderId") Long orderId) {
        Order order = orderService.getById(orderId);
        if (order == null || !order.getStatus().equals(OrderConstant.PENDING_PAYMENT)) {
            return Result.error("");
        }
        order.setStatus(OrderConstant.PAID);
        order.setUpdateTime(new Date());
        orderService.updateById(order);
        return Result.success();
    }
}
